package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;


    // Request URL: http://192.168.200.1:8500/api/album/trackInfo/uploadTrack


    @PostMapping(value = "/uploadTrack")
    @Operation(summary = "上传声音")
    public Result uploadTrack(MultipartFile file) {
        Map<String, Object> map = trackInfoService.uploadTrack(file);
        return Result.ok(map);
    }


    // Request URL: http://192.168.200.1:8500/api/album/trackInfo/saveTrackInfo

    @PostMapping("/saveTrackInfo")
    @Operation(summary = "保存声音信息")
    @TingshuLogin
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    // Request URL: http://192.168.200.1:8500/api/album/trackInfo/findUserTrackPage/1/10
    @PostMapping("/findUserTrackPage/{pn}/{pz}")
    @Operation(summary = "查询登录用户的声音分页展示")
    @TingshuLogin
    public Result findUserTrackPage(@PathVariable(value = "pn") Long pn,
                                    @PathVariable(value = "pz") Long pz,
                                    @RequestBody TrackInfoQuery trackInfoQuery) {
        // 1.构建分页对象
        IPage<TrackListVo> trackInfoVoPage = new Page<>(pn, pz);
        trackInfoQuery.setUserId(AuthContextHolder.getUserId()); // 给条件对象赋值用户
        trackInfoVoPage = trackInfoService.findUserTrackPage(trackInfoVoPage, trackInfoQuery);
        return Result.ok(trackInfoVoPage); // 前端未来要的是分页对象里面的对象
    }

    // Request URL: http://192.168.200.1:8500/api/album/trackInfo/getTrackInfo/51944

    @GetMapping(value = "/getTrackInfo/{trackId}")
    @Operation(summary = "回显声音")
    public Result getTrackInfo(@PathVariable(value = "trackId") Long trackId) {
        TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
        return Result.ok(trackInfo);
    }

    // Request URL: http://192.168.200.1:8500/api/album/trackInfo/updateTrackInfo/51944
    @PutMapping("/updateTrackInfo/{trackId}")
    @Operation(summary = "修改声音")
    @TingshuLogin   // 校验用户是否登录
    public Result updateTrackInfo(@RequestBody TrackInfoVo trackInfoVo,
                                  @PathVariable(value = "trackId") Long trackId) {

        trackInfoService.updateTrackInfo(trackInfoVo, trackId);
        return Result.ok();
    }


}

